<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <!--      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>-->
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>客户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--用户列表卡片-->
    <el-card class="box-card">
      <el-form :inline="true" :model="customer" class="demo-form-inline" label-width="70px">
        <el-form-item label="姓名" label-width="70px">
          <el-input clearable v-model="customer.username" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="70px">
          <el-input clearable v-model="customer.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="地址" label-width="70px" >
          <el-input clearable v-model="customer.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="customer.sex" label="0">男</el-radio>
          <el-radio v-model="customer.sex" label="1">女</el-radio>
          <el-radio v-model="customer.sex" label>全部</el-radio>
        </el-form-item>
        <el-form-item label="电话" label-width="70px">
          <el-input clearable v-model="customer.phone" placeholder="请输入电话"></el-input>
        </el-form-item>

        <el-form-item style="margin-left: 30px">
          <el-button icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" icon="el-icon-search">查询</el-button>
          <el-button type="success" icon="el-icon-plus">添加</el-button>
        </el-form-item>
      </el-form>
      <!--      表格内容显示区域-->
      <el-table
        v-loading="loading"
        :data="cusList"
        border
        max-height="600"
        style="width: 100%;">
        <el-table-column
          prop="id"
          label="#"
          width="50">
        </el-table-column>
        <el-table-column
          prop="username"
          label="姓名"
          width="110">
        </el-table-column>
        <el-table-column prop="sex" :formatter="showSex" label="性别" width="80">
          <template slot-scope="scope">
            <el-tag size="small" type="success" v-if="scope.row.sex===0">男</el-tag>
            <el-tag size="small"  type="warning" v-else>女</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话"
          width="180">
        </el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          width="440"
          :show-overflow-tooltip='true'>
        </el-table-column>
        <el-table-column
          label="操作"
          width="120">
          <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top:15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getCusList } from '../../api/customer'

export default {
  name: 'Customer',
  data () {
    return {
      //当前页
      current: 1,
      size: 6,
      total: 100,
      loading: true,
      customer: {
        username: '',
        email: '',
        sex: '',
        phone: '',
        address: '',
      },
      cusList: [],
      // 角色对话框的可见性
      dialogVisible: false,
      // 角色
      roles: [],
      // 用户拥有的角色
      hasRoleIds: [],
      // 保存当前授权的用户ID
      uid: '',
    }
  },
  created () {
    //创建组件的时候调用查询所有用户的方法
    this.getUsers()
    setTimeout(() => {
      this.loading = false
    }, 500)
  },
  methods: {
    // 每页条数改变
    handleSizeChange (val) {
      this.size = val
      this.getUsers()
      // console.log(`每页 ${val} 条`);
    },
    // 当前页码改变
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.current = val
      this.getUsers()
    },
    async getUsers () {

      const res = await getCusList(this.current, this.size, this.customer)
      if (res.code !== 200) {
        return this.$message.error('获取客户列表失败:' + res.message)
      }
      this.cusList = res.data.cusList
      this.total = res.data.total
    },
    /**
     * 显示用户性别
     */
    showSex (row, column) {
      return row.sex == 0 ? ' 男' : '女'
    },
  }
}
</script>

<style scoped>

</style>
